﻿#include "capstatuswidget.h"

QCapStatusWidget::QCapStatusWidget(QWidget *parent) : QWidget(parent)
{
    _i32FrameCnt = 0;
    _i32Frame11Cnt = 0;
    m_bSaveFile = false;

    _cmdProcStatusMap[0] = "正常";
    _cmdProcStatusMap[1] = "开使能响应正常";
    _cmdProcStatusMap[2] = "关使能相应正常";
    _cmdProcStatusMap[3] = "速度门限设置响应正常";
    _cmdProcStatusMap[4] = "电流门限设置响应正常";
    _cmdProcStatusMap[5] = "过流门限设置响应正常";
    _cmdProcStatusMap[6] = "加速度门限设置响应正常";
    _cmdProcStatusMap[7] = "角度位置偏置设置响应正常";
    _cmdProcStatusMap[8] = "零位偏置设置响应正常";
    _cmdProcStatusMap[9] = "滤波系数设置响应正常";
    _cmdProcStatusMap[0x0A] = "超速门限设置响应正常";
    _cmdProcStatusMap[0x0B] = "电机绕组切换响应正常";
    _cmdProcStatusMap[0x0C] = "解锁器加电响应正常";
    _cmdProcStatusMap[0x0D] = "机械角度限位设置响应正常";
    _cmdProcStatusMap[0x0E] = "非本轴命令";
    _cmdProcStatusMap[0x0F] = "发送指令未响应";

    this->setStyleSheet("background-color:transparent;");


    memset(&_rcv_capdata,0,sizeof(_rcv_capdata));

    InitRcvDataTabelwidget();

    _pSaveDataFileCheckBox = new QCheckBox("保存实时数据",this);
    _pSaveDataFileCheckBox->setStyleSheet("color:rgb(255,255,255);font-family:'Microsoft YaHei';font-size:24px;");


    QLabel* plabel = new QLabel(this);
    plabel->setText("接收数据显示");
    plabel->setStyleSheet("QLabel{color:rgb(255,255,255);font-family:'Microsoft YaHei';font-size:24px;}");
    plabel->setMaximumHeight(30);


    QGridLayout* playout = new QGridLayout(this);
    playout->addWidget(plabel,0,0,1,1);
    playout->addWidget(_pSaveDataFileCheckBox,0,5,1,1);

    playout->addWidget(_pRcvDataTableWidget,1,0,1,6);

    playout->setRowStretch(0,1);
    playout->setRowStretch(1,6);

    playout->setSpacing(0);
    playout->setMargin(0);

//    st_rcvdata l_st_rcvdata;

    QVariant var;
    var.setValue(_rcv_capdata);

    onRcvData(var);


    connect(_pSaveDataFileCheckBox,&QCheckBox::clicked,this,&QCapStatusWidget::onSaveDataFileCheck);

}
/**
* @brief
* @note
*
*/
void QCapStatusWidget::InitRcvDataTabelwidget()
{
    _pRcvDataTableWidget = new QTableWidget(this);

    QString sTableStyle = "QTableWidget{gridline-color: rgb(150, 150, 150);border:0px solid;border-top:1px solid rgb(150, 150, 150);";
    sTableStyle = sTableStyle + "font-size:20px;font-family: 'Microsoft YaHei';color:rgb(255,255,255);}";
    sTableStyle = sTableStyle + "QTableWidget::Item{border:0px solid rgb(150, 150, 150);border-bottom:1px solid rgb(150, 150, 150);}";

    _pRcvDataTableWidget->setStyleSheet(sTableStyle);
    _pRcvDataTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑
    _pRcvDataTableWidget->horizontalHeader()->setStretchLastSection(true);//最后一列充满
    _pRcvDataTableWidget->horizontalHeader()->hide();
    _pRcvDataTableWidget->verticalHeader()->hide();
    _pRcvDataTableWidget->setShowGrid(false);
    _pRcvDataTableWidget->setColumnCount(9);
    _pRcvDataTableWidget->setRowCount(6);
//    _pRcvDataTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);    //先自适应宽度
    _pRcvDataTableWidget->setColumnWidth(2,150);
    _pRcvDataTableWidget->setColumnWidth(5,150);


    int nRow = 0;
    _pRcvDataTableWidget->setRowHeight(nRow,60);
    _pRcvDataTableWidget->setItem(nRow,0,new QTableWidgetItem("帧计数:"));
    _pRcvDataTableWidget->setItem(nRow,1,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setSpan(nRow,0,2,1);
    _pRcvDataTableWidget->setSpan(nRow,1,2,1);
    _pRcvDataTableWidget->setItem(nRow,2,new QTableWidgetItem("方位遥测信息:"));
    _pRcvDataTableWidget->setItem(nRow,3,new QTableWidgetItem(""));
    _pRcvDataTableWidget->setItem(nRow,5,new QTableWidgetItem("指令接收状态:"));
    _pRcvDataTableWidget->setItem(nRow,6,new QTableWidgetItem(""));
    _pRcvDataTableWidget->setSpan(nRow,3,1,2);
    _pRcvDataTableWidget->setSpan(nRow,6,1,2);
    nRow++;

    _pRcvDataTableWidget->setRowHeight(nRow,60);
    _pRcvDataTableWidget->setItem(nRow,2,new QTableWidgetItem("俯仰遥测信息:"));
    _pRcvDataTableWidget->setItem(nRow,3,new QTableWidgetItem(""));
    _pRcvDataTableWidget->setItem(nRow,5,new QTableWidgetItem("指令执行状态:"));
    _pRcvDataTableWidget->setItem(nRow,6,new QTableWidgetItem(""));
    _pRcvDataTableWidget->setSpan(nRow,3,1,2);
    _pRcvDataTableWidget->setSpan(nRow,6,1,2);
    nRow++;


    _pRcvDataTableWidget->setRowHeight(nRow,60);
    _pRcvDataTableWidget->setItem(nRow,0,new QTableWidgetItem("方位轴"));
    _pRcvDataTableWidget->setSpan(nRow,0,2,1);
    _pRcvDataTableWidget->setItem(nRow,1,new QTableWidgetItem("指令值:"));
    _pRcvDataTableWidget->setItem(nRow,2,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,3,new QTableWidgetItem("角度值:"));
    _pRcvDataTableWidget->setItem(nRow,4,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,5,new QTableWidgetItem("A相电流:"));
    _pRcvDataTableWidget->setItem(nRow,6,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,7,new QTableWidgetItem("Q相电流值:"));
    _pRcvDataTableWidget->setItem(nRow,8,new QTableWidgetItem("0"));

    nRow++;

    _pRcvDataTableWidget->setRowHeight(nRow,60);
    _pRcvDataTableWidget->setItem(nRow,1,new QTableWidgetItem("指令计数:"));
    _pRcvDataTableWidget->setItem(nRow,2,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,3,new QTableWidgetItem("速度值:"));
    _pRcvDataTableWidget->setItem(nRow,4,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,5,new QTableWidgetItem("C相电流:"));
    _pRcvDataTableWidget->setItem(nRow,6,new QTableWidgetItem("0"));
    nRow++;

    _pRcvDataTableWidget->setRowHeight(nRow,60);
    _pRcvDataTableWidget->setItem(nRow,0,new QTableWidgetItem("俯仰轴"));
    _pRcvDataTableWidget->setSpan(nRow,0,2,1);
    _pRcvDataTableWidget->setItem(nRow,1,new QTableWidgetItem("指令值:"));
    _pRcvDataTableWidget->setItem(nRow,2,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,3,new QTableWidgetItem("角度值:"));
    _pRcvDataTableWidget->setItem(nRow,4,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,5,new QTableWidgetItem("A相电流:"));
    _pRcvDataTableWidget->setItem(nRow,6,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,7,new QTableWidgetItem("Q相电流值:"));
    _pRcvDataTableWidget->setItem(nRow,8,new QTableWidgetItem("0"));

    nRow++;

    _pRcvDataTableWidget->setRowHeight(nRow,60);
    _pRcvDataTableWidget->setItem(nRow,1,new QTableWidgetItem("指令计数:"));
    _pRcvDataTableWidget->setItem(nRow,2,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,3,new QTableWidgetItem("速度值:"));
    _pRcvDataTableWidget->setItem(nRow,4,new QTableWidgetItem("0"));
    _pRcvDataTableWidget->setItem(nRow,5,new QTableWidgetItem("C相电流:"));
    _pRcvDataTableWidget->setItem(nRow,6,new QTableWidgetItem("0"));
    nRow++;

}


/**
* @brief
* @note
*
*/
void QCapStatusWidget::onRcvData(QVariant var)
{
    st_rcvdata l_rcv_data = var.value<st_rcvdata>();

    _rcv_capdata = l_rcv_data;

    if(m_bSaveFile == true)
    {
        int nlen = sizeof(st_rcvdata);
        uchar* pdata = (uchar*)&l_rcv_data;
        for(int i = 0; i < nlen;i++)
        {
            m_sRcvData = m_sRcvData + QString("0x%1 ").arg((uchar)(pdata[i]),2,16);
        }
        m_sRcvData = m_sRcvData +"\n";

        if(m_sRcvData.size() > 1000)
        {
            writeDataToFile(m_sRcvData);
            m_sRcvData = "";
        }
    }




    _i32FrameCnt++;
    if(_i32FrameCnt% 50 == 0)
    {
      showRcvData();
    }

    if(_rcv_capdata.loopcmd == 0x11)
    {
        _i32Frame11Cnt++;
        if(_i32FrameCnt% 50 == 0)
        {
          showRcv11Data();
        }


    }

}

/**
* @brief
* @note
*
*/
void QCapStatusWidget::showRcvData()
{
    int nRow = 0;
    _pRcvDataTableWidget->item(nRow,1)->setText(QString::number(_rcv_capdata.framecnt));
    _pRcvDataTableWidget->item(nRow,3)->setText(parseAzimuthInfo(_rcv_capdata.azimuthInfo));
    _pRcvDataTableWidget->item(nRow,6)->setText(parseCmdRcvStatus(_rcv_capdata.cmdrcvstatus));
    nRow++;
    _pRcvDataTableWidget->item(nRow,3)->setText(parsePitchInfo(_rcv_capdata.pitchInfo));
    _pRcvDataTableWidget->item(nRow,6)->setText(parseCmdProcStatus(_rcv_capdata.cmdprocstatus));
    nRow++;

    //方位轴
    fourchar l_data;
    l_data.data[0] = _rcv_capdata.azimuthcmd_value[2];
    l_data.data[1] = _rcv_capdata.azimuthcmd_value[1];
    l_data.data[2] = _rcv_capdata.azimuthcmd_value[0];
    l_data.data[3] = 0;
    _pRcvDataTableWidget->item(nRow,2)->setText(QString::number(l_data.ui32data*360.0/16777216));//指令值

    l_data.data[0] = _rcv_capdata.azimuthresponse_value[2];
    l_data.data[1] = _rcv_capdata.azimuthresponse_value[1];
    l_data.data[2] = _rcv_capdata.azimuthresponse_value[0];
    l_data.data[3] = 0;
    _pRcvDataTableWidget->item(nRow,4)->setText(QString::number(l_data.ui32data*360.0/16777216));//角度值

    _pRcvDataTableWidget->item(nRow,6)->setText(QString::number(_rcv_capdata.azimuthcurrent_A.i16data*0.0001));//A相电流
    nRow++;
    _pRcvDataTableWidget->item(nRow,2)->setText(QString::number(_rcv_capdata.azimuthcmdcnt));//指令计数
    _pRcvDataTableWidget->item(nRow,6)->setText(QString::number(_rcv_capdata.azimuthcurrent_C.i16data*0.0001));//C相电流
    nRow++;

    //俯仰轴
    l_data.data[0] = _rcv_capdata.pitchcmd_value[2];
    l_data.data[1] = _rcv_capdata.pitchcmd_value[1];
    l_data.data[2] = _rcv_capdata.pitchcmd_value[0];
    l_data.data[3] = 0;
    _pRcvDataTableWidget->item(nRow,2)->setText(QString::number(l_data.ui32data*360.0/16777216));//指令值

    l_data.data[0] = _rcv_capdata.pitchresponse_value[2];
    l_data.data[1] = _rcv_capdata.pitchresponse_value[1];
    l_data.data[2] = _rcv_capdata.pitchresponse_value[0];
    l_data.data[3] = 0;
    _pRcvDataTableWidget->item(nRow,4)->setText(QString::number(l_data.ui32data*360.0/16777216));//角度值
    _pRcvDataTableWidget->item(nRow,6)->setText(QString::number(_rcv_capdata.pitchcurrent_A.i16data*0.0001));//A相电流

    nRow++;
    _pRcvDataTableWidget->item(nRow,2)->setText(QString::number(_rcv_capdata.pitchcmdcnt));//指令计数
    _pRcvDataTableWidget->item(nRow,6)->setText(QString::number(_rcv_capdata.pitchcurrent_C.i16data*0.0001));//C相电流
}

/**
* @brief
*
**/
void QCapStatusWidget::showRcv11Data()
{
    //方位轴
    //Q相电流
    twochar l_data;
    l_data.data[0] = _rcv_capdata.loopcmd_data.loopdata_11.azimuth_currentQ_response_value.data[1];
    l_data.data[1] = _rcv_capdata.loopcmd_data.loopdata_11.azimuth_currentQ_response_value.data[0];
    float lfdata = l_data.i16data /1000.0;
    _pRcvDataTableWidget->item(2,8)->setText(QString::number(lfdata,'f',4));

    //速度值
    l_data.data[0] = _rcv_capdata.loopcmd_data.loopdata_11.azimuth_speed_response_value.data[1];
    l_data.data[1] = _rcv_capdata.loopcmd_data.loopdata_11.azimuth_speed_response_value.data[0];
    lfdata = l_data.i16data /100.0;
    _pRcvDataTableWidget->item(3,4)->setText(QString::number(lfdata,'f',4));

    //俯仰轴
    //Q相电流
    l_data.data[0] = _rcv_capdata.loopcmd_data.loopdata_11.pitch_currentQ_response_value.data[1];
    l_data.data[1] = _rcv_capdata.loopcmd_data.loopdata_11.pitch_currentQ_response_value.data[0];
    lfdata = l_data.i16data /1000.0;
    _pRcvDataTableWidget->item(4,8)->setText(QString::number(lfdata,'f',4));

    //速度值
    l_data.data[0] = _rcv_capdata.loopcmd_data.loopdata_11.pitch_speed_response_value.data[1];
    l_data.data[1] = _rcv_capdata.loopcmd_data.loopdata_11.pitch_speed_response_value.data[0];
    lfdata = l_data.i16data /100.0;
    _pRcvDataTableWidget->item(5,4)->setText(QString::number(lfdata,'f',4));

}
/**
* @brief
* @note
*
*/
QString QCapStatusWidget::parseAzimuthInfo(uchar data)
{
    QString sInfo = "正常";
    if(data != 0x0)
    {
        sInfo = "异常";
    }
    return sInfo;
}
/**
* @brief
* @note
*
*/
QString QCapStatusWidget::parsePitchInfo(uchar data)
{
    QString sInfo = "正常";
    if(data != 0x0)
    {
        sInfo = "异常";
    }
    return sInfo;
}

/**
* @brief
* @note
*
*/
QString QCapStatusWidget::parseCmdRcvStatus(uchar data)
{
    QString sInfo = "正常";

    if((data&0x80) == 0x80)
    {
        sInfo = "";
        if((data&0x40) == 0x40)
        {
            sInfo =  sInfo + "帧类型错误 ";
        }
        if((data&0x20) == 0x20)
        {
            sInfo =  sInfo + "方位数据类型错误 ";
        }
        if((data&0x10) == 0x10)
        {
            sInfo = sInfo + "俯仰数据类型错误 ";
        }
        if((data&0x08) == 0x08)
        {
            sInfo = sInfo + "校验和错误 ";
        }
        if((data&0x04) == 0x04)
        {
            sInfo = sInfo+ "相对编码器 ";
        }
        if((data&0x02) == 0x02)
        {
            sInfo = sInfo +"参数空间出错 ";
        }
        if((data&0x01) == 0x01)
        {
            sInfo = sInfo + "程序运行在上注空间 ";
        }
    }
    return sInfo;
}
/**
* @brief
* @note
*
*/
QString QCapStatusWidget::parseCmdProcStatus(uchar data)
{
    uchar l_azimuth_status = ((data&0xf0)>>4);
    uchar l_pitch_status = (data&0x0f);

    QString sInfo;
    sInfo = "方位:"+ _cmdProcStatusMap[l_azimuth_status]+"\n";
    sInfo = sInfo+ "俯仰:"+ _cmdProcStatusMap[l_pitch_status];

    return sInfo;
}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCapStatusWidget::onSaveDataFileCheck()
{
    if(_pSaveDataFileCheckBox->checkState() == Qt::CheckState::Checked)
    {
        m_sRcvData="";
        QString sFileFullPath = QDateTime::currentDateTime().toString("yyyyMMddhhmmss")+".txt";
        sFileFullPath = QApplication::applicationDirPath()+"/data/"+sFileFullPath;
        m_savefile.setFileName(sFileFullPath);
        m_bSaveFile = true;
    }
    else
    {
        m_bSaveFile = false;
        m_savefile.close();
    }

}
/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCapStatusWidget::writeDataToFile(QString str)
{
    if(m_savefile.fileName().isEmpty())
    {
        return;
    }

    if(!m_savefile.isOpen())
    {
        m_savefile.open(QFile::ReadWrite|QFile::Append);
    }

    m_savefile.write(str.toLocal8Bit());
}
